// 引入模块
import Koa from 'koa'
import KoaStatic from 'koa-static'
import Router from 'koa-router'
import bodyParser from 'koa-bodyparser'
import myrouter from '../router/index'
import cors from 'koa2-cors'

// 具体参数我们在后面进行解释


const app = new Koa()
// const router = new Router();

app.use(bodyParser());

//设置跨域
app.use(cors({
    origin: function (ctx) {
        if (ctx.url === '/test') {
            return "*"; // 允许来自所有域名请求
        }
        return 'http://127.0.0.1:8080'; // 这样就能只允许 http://localhost:8080 这个域名的请求了
    },
    exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
    maxAge: 5,
    credentials: true,
    allowMethods: ['GET', 'POST', 'DELETE'],
    allowHeaders: ['Content-Type', 'Authorization', 'Accept'],
}))
// app.use(cors())

// 使用 bodyParser 和 KoaStatic 中间件

//app.use(KoaStatic(__dirname + '../public'));




// 路由设置test
// router.use('', myrouter.routes());


app.use(myrouter.routes()).use(myrouter.allowedMethods());

app.listen(4000, console.log('server is running'))
